<template>
  <div>
    <el-dialog v-model="operateShow" :title="valueType ? '申请删除' : '申请退款'" width="500px" :before-close="close"
      :close-on-click-modal="false" @open="open()">
      <el-form :model="queryForm" v-loading="formLoading" :rules="rules" ref="queryRef" label-width="100px"
        class="demo-form-inline">
        <el-row>
          <el-col :span="23">
            <el-form-item label="姓名" prop="name">
              <el-input v-model.trim="queryForm.name" :disabled="true" placeholder="请输入" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="23">
            <el-form-item label="课程" prop="cur_name">
              <el-input v-model.trim="queryForm.cur_name" :disabled="true" placeholder="请输入" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="23">
            <el-form-item label="订单金额" prop="order_amount">
              <el-input v-model.trim="queryForm.order_amount" :disabled="true" placeholder="请输入" clearable />
            </el-form-item>
          </el-col>
          <el-col v-if="!valueType" :span="23">
            <el-form-item label="退款金额" prop="refound_anount">
              <el-input v-model.trim="queryForm.refound_anount" @input="validateNumber(queryForm, 'refound_anount')"
                placeholder="请输入" clearable />
            </el-form-item>
          </el-col>
          <el-col :span="23">
            <el-form-item label="备注" prop="remarks">
              <el-input v-model.trim="queryForm.remarks" :rows="4" type="textarea" show-word-limit maxlength="500"
                placeholder="请输入" clearable />
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="close(false)">取消</el-button>
          <el-button type="primary" @click="submit(queryRef)" :loading="submitLoading">
            确定
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>
<script lang="ts">
import { applyDelete, applyRefund } from '@/api/goodsOrder/addOrder'
import { nextTick } from 'vue'
import useCurrentInstance from '@/utils/useCurrentInstance'
import { defineComponent, reactive, ref, watch } from '@vue/runtime-core'
import { ElMessage } from 'element-plus'
export default defineComponent({
  props: {
    valueShow: {
      type: Boolean,
      default: false,
    },
    valueRow: {
      type: Object,
      default: () => { }
    },
    valueType: {
      type: Boolean,
      default: false
    }
  },
  setup(props, { emit }) {
    // 变量区域
    const { proxy } = useCurrentInstance()
    const operateShow = ref(false)
    const formLoading = ref(false)
    const submitLoading = ref(false)
    const queryRef: any = ref(null)
    const queryForm: any = ref({
      order_id: null,
      name: null,
      cur_name: null,
      order_amount: null,
      refound_anount: null,
      remarks: null,
    })
    const rule = { required: true, message: '必填项', trigger: 'blur' }
    const rules = {
      refound_anount: [rule],
      remarks: [rule],
    }

    // 方法区域
    const open = () => {
      nextTick(() => {
        queryForm.value.order_id = props.valueRow.order_id
        queryForm.value.name = props.valueRow.stu_name
        queryForm.value.cur_name = props.valueRow.cur_name
        queryForm.value.order_amount = props.valueRow.cur_price
      })
    }

    const close = (val) => {
      emit('closeOperate', val)
      nextTick(() => {
        queryRef.value.resetFields()
      })
    }

    const submit = (formRef) => {
      formRef.validate((valid) => {
        if (!valid) return
        const params = {
          order_id: queryForm.value.order_id,
          refound_anount: queryForm.value.refound_anount,
          remarks: queryForm.value.remarks
        }
        const submit = props.valueType ? applyDelete : applyRefund
        formLoading.value = true
        submitLoading.value = true
        submit(params).then((res: any) => {
          close(true)
          if (props.valueType) {
            ElMessage.success('申请删除成功')
          } else {
            ElMessage.success('申请退款成功')
          }
        }).finally(() => {
          formLoading.value = false
          submitLoading.value = false
        })
      })
    }

    const validateNumber = (obj, val) => {
      // 用正则表达式检查输入是否合法
      // const regex = /^\d*\.?\d{0,2}$/
      // 如果输入不合法，移除最后一位字符
      // !regex.test(obj[val]) && (obj[val] = obj[val].slice(0, -1))
      obj[val] = obj[val].replace(/[^0-9]/g, '')
    }

    // 钩子函数区域
    watch(
      () => props.valueShow,
      (newVal) => {
        operateShow.value = newVal
      }
    )

    // 方法回调区域

    // return返回值
    return {
      operateShow,
      formLoading,
      submitLoading,
      queryRef,
      queryForm,
      rules,
      open,
      close,
      submit,
      validateNumber,
    }
  },
})
</script>
